﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ForrajeriaBCL;
using ForrajeriaDB;


namespace ForrajeriaFRONT.ABM
{
    public partial class Animal : System.Web.UI.Page
    {
        private AnimalBCL objBC;
        private AnimalMO objEditar;

        public Animal() //constructor
        {
            objBC= new AnimalBCL();

        } 
        protected void Page_Load(object sender, EventArgs e)
        {
            cargarLista();
            lblResultado.Text = "";
            btnGuardar.Focus();

        }

       
        protected void cargarLista()
        {
            gridDatos.DataSource = null;
            List<AnimalMO> lista = objBC.GetAll();
            gridDatos.DataSource = lista;
            gridDatos.DataBind();

        }

        #region Eventos BOTONES

        

        
        protected void btnGuardar_Click(Object sender, EventArgs e)
        {
            objEditar = (AnimalMO)ViewState["objEdi"];
            if (objEditar == null)//INSERT
            {
                
                objEditar=new AnimalMO(null,txtNombre.Text);
                string resu=objBC.Adicionar(objEditar);

                if (resu == "1")
                {
                    lblResultado.ForeColor = System.Drawing.Color.Blue;
                    ValidationSumary1.ShowSummary = true;
                    lblResultado.Text = "Guardado con Exito";
                    cargarLista();

                }
                else 
                {
                    lblResultado.ForeColor = System.Drawing.Color.Red;
                    ValidationSumary1.ShowSummary = true;
                    lblResultado.Text = resu;
                }
            }
            else // UPDATE
            {
                AnimalMO objNuevo = new AnimalMO(objEditar.CodigoAnimal, txtNombre.Text);
                string resu = objBC.Actualizar(objNuevo, objEditar);

                if (resu == "1")
                {
                    lblResultado.ForeColor = System.Drawing.Color.Blue;
                    ValidationSumary1.ShowSummary = true;
                    lblResultado.Text = "Guardado con Exito";
                    cargarLista();
                }
                else 
                {
                    lblResultado.ForeColor = System.Drawing.Color.Red;
                    lblResultado.Text = resu;
                }
                ViewState["objEdi"] = null;
                gridDatos.SelectedIndex = -1;
                txtNombre.Text = "";
            }
 
        }

        #endregion

        protected void gridDatos_SelectedIndexChanging1(object sender, GridViewSelectEventArgs e)
        {
            int ind = e.NewSelectedIndex;

            objEditar = new AnimalMO();
            objEditar.CodigoAnimal = Int32.Parse(gridDatos.Rows[ind].Cells[0].Text);
            objEditar.Nombre = gridDatos.Rows[ind].Cells[1].Text;
            ViewState["objEdi"] = objEditar;

            txtNombre.Text = gridDatos.Rows[ind].Cells[1].Text;
        }

        protected void gridDatos_RowDeleting1(object sender, GridViewDeleteEventArgs e)
        {

            txtNombre.Text = "";
            gridDatos.SelectedIndex = -1;
            int ind = e.RowIndex;
            objEditar = new AnimalMO();
            objEditar.CodigoAnimal = Int32.Parse(gridDatos.Rows[ind].Cells[0].Text);
            objEditar.Nombre = gridDatos.Rows[ind].Cells[1].Text;
            string resu = objBC.Eliminar(objEditar);

            if (resu != "1")
            {
                lblResultado.Text = resu;
                lblResultado.ForeColor = System.Drawing.Color.Red;
            }
            cargarLista();
        }
    }
}